#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>

#include "net.h"

static void _display_host(struct sockaddr_in *addr) {
  char ip[INET_ADDRSTRLEN];

  memset(ip, 0, sizeof(ip));
  inet_ntop(AF_INET, &addr->sin_addr.s_addr, ip, sizeof(ip));
  printf("%s:%d", ip, ntohs(addr->sin_port));
}

static void _display_flow_header(flow_t *flow) {
  printf("@%ld:%06ld ", flow->when.tv_sec, flow->when.tv_usec);
  _display_host(&flow->connector);
  printf(" -> ");
  _display_host(&flow->connectee);
  printf(" [%s]\n", (flow->protocol == IPPROTO_UDP ? "udp" : "tcp"));
}

int main(int argc, char **argv) {
  flowset_t *set;
  flow_t *flow;

  set = flowset_load(argv[1], (argc == 3 ? argv[2] : NULL));

  TAILQ_FOREACH(flow,&set->flows,next) {
    _display_flow_header(flow);
  }

  return 0;
}
